package blacknWhite.Libraries;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DecimalFormat;
import java.util.Random;

import com.nullwire.trace.G;

import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

public class Utils {
	public static Handler handler = new Handler();
	
	public static void showToast(Context ctx, String text) {
		Toast.makeText(ctx, text, Toast.LENGTH_LONG).show();
	}
	
	static public String customFormat(String pattern, float s ) {
        DecimalFormat myFormatter = new DecimalFormat(pattern);
        String stringformatoutput = myFormatter.format(s);
        return stringformatoutput;
     } 

	public static void LogException(Throwable ex) {
		saveUncaughtException(Thread.currentThread(), ex);
	}

	private static void saveUncaughtException(Thread t, Throwable e) {
		// Here you should have a more robust, permanent record of problems
		final Writer result = new StringWriter();
		final PrintWriter printWriter = new PrintWriter(result);
		e.printStackTrace(printWriter);
		try {
			// Random number to avoid duplicate files
			Random generator = new Random();
			int random = generator.nextInt(99999);
			// Embed version in stacktrace filename
			String filename = G.APP_VERSION + "-" + Integer.toString(random);
			// Write the stacktrace to disk
			BufferedWriter bos = new BufferedWriter(new FileWriter(G.FILES_PATH
					+ "/" + filename + ".stacktrace"));
			bos.write(G.ANDROID_VERSION + "\n");
			bos.write(G.PHONE_MODEL + "\n");
			bos.write(result.toString());
			bos.flush();
			// Close up everything
			bos.close();
		} catch (Exception ebos) {
			// Nothing much we can do about this - the game is over
			ebos.printStackTrace();
		}
	}
}
